package com.ftg.learn.mybatis.chapter3.service.impl;

import com.ftg.learn.mybatis.chapter2.util.TipMess;
import com.ftg.learn.mybatis.chapter3.config.SessionUtil;
import com.ftg.learn.mybatis.chapter3.entity.Sale;
import com.ftg.learn.mybatis.chapter3.entity.SaleFramer;
import com.ftg.learn.mybatis.chapter3.mapper.SaleFramerMapper;
import com.ftg.learn.mybatis.chapter3.mapper.SaleMapper;
import com.ftg.learn.mybatis.chapter3.service.ISale;
import com.ftg.learn.mybatis.chapter3.vo.SaleAndFramer;

import java.util.List;
import java.util.Objects;

/**
 * 实现类
 * @author kangjx
 * @createTime 2022/10/13
 */
public class SaleImpl implements ISale {

    /**
     * 接口是不能直接实例化的
     *  声明接口 拿到的实现类
     *  代理设计模式 new ProxyInstance(); : mybatis已经完成了
     */
    SaleMapper saleMapper;
    SaleFramerMapper saleFramerMapper;

    SessionUtil sessionUtil = new SessionUtil();

    public SaleImpl(){
        saleMapper = sessionUtil.getAutoCommitSession().getMapper(SaleMapper.class);
        saleFramerMapper = sessionUtil.getAutoCommitSession().getMapper(SaleFramerMapper.class);
    }

    @Override
    public List<Sale> showall() {
        return saleMapper.showall();
    }

    @Override
    public List<Sale> showallWithPara(Integer id, String name) {
        return saleMapper.showallWithPara(id,name);
    }

    @Override
    public int saveSale(Sale sale) {
        SaleFramer saleFramer = saleFramerMapper.findById(sale.getFid());
        if(Objects.isNull(saleFramer)){
            throw new RuntimeException(TipMess.FRAMEER_NOT_EXIST);
        }
        return saleMapper.saveSale(sale);
    }

    @Override
    public List<SaleAndFramer> showSaleInfoWithFramer() {
        return saleMapper.showSaleInfoWithFramer();
    }

}
